/**
 * 
 */
package com.taobao.snowsnow.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.taobao.snowsnow.hibernate.dao.BowDao;
import com.taobao.snowsnow.hibernate.pojo.Bow;

/**
 * @author Tux
 *
 * 创建时间: 2011-4-29 上午09:45:11
 */
@SuppressWarnings("unchecked")
public class BowDaoImpl extends BaseDaoImpl implements BowDao {
	
	public Bow findById(int id) throws Exception {
		return (Bow) getHibernateTemplate().get(Bow.class, id);
	}
	
	
	public List<Bow> findByName(String name) throws Exception {
		return super.findByProperty(Bow.class, "name", name);
	}

	public List<Bow> findAll() throws Exception {
		return getHibernateTemplate().find("from Bow");
	}

	public long getTotal() throws Exception {
		return super.countByCriteria(Bow.class);
	}

	public List<Bow> paging(int page, int limit) throws Exception {
		return super.findByCriteriaByPage(Bow.class, page, limit);
	}

	@Override
	public Bow getFirstObject() throws Exception {
		Query query = getSession().createQuery("from Bow as b where b.id = (select min(id) from Bow)");
		List<Bow> list = query.list();
		if(list.size() !=0)
			return (Bow) list.get(0);
		else return null;
	}

	@Override
	public Bow getLastObject() throws Exception {
		Query query = getSession().createQuery("from Bow as b where b.id = (select max(id) from Bow)");
		List<Bow> list = query.list();
		if(list.size() !=0)
			return (Bow) list.get(0);
		else return null;
	}
}
